package com.telchina.workorder.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.telchina.common.annotation.Log;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.domain.WoAttachment;
import com.telchina.common.enums.BusinessType;
import com.telchina.common.mapper.WoAttachmentMapper;
import com.telchina.common.utils.DateUtils;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.common.utils.poi.ExcelUtil;
import com.telchina.workorder.domain.WoDisregardKpi;
import com.telchina.workorder.domain.vo.WoDisregardKpiReqVO;
import com.telchina.workorder.domain.vo.WoDisregardKpiVO;
import com.telchina.workorder.service.IWoDisregardKpiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 不及考核申请Controller
 *
 * @author konghao
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/workorder/disregardKpi")
public class WoDisregardKpiController extends BaseController {
    @Autowired
    private IWoDisregardKpiService disregardKpiService;
    @Autowired
    private WoAttachmentMapper woAttachmentMapper;

    /**
     * 分页查询不及考核申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:list')")
    @GetMapping("/list")
    public AjaxResult list(WoDisregardKpi disregardKpi) {
        startPage();
//        IPage<WoDisregardKpi> list = disregardKpiService.selectList(startPage(), disregardKpi);
        List<WoDisregardKpi> list = disregardKpiService.selectListAll(disregardKpi);
        return getDataTable(list);
    }

    /**
     * 查询不及考核申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll(WoDisregardKpi disregardKpi) {
        List<WoDisregardKpi> list = disregardKpiService.selectListAll(disregardKpi);
        List<WoDisregardKpiVO> listVO = new ArrayList<>();
        // 获取对应的附件信息
        List<String> superviseIds = list.stream().map(WoDisregardKpi::getId).collect(Collectors.toList());
        List<WoAttachment> woAttachmentList = Collections.emptyList();
        if(CollUtil.isNotEmpty(superviseIds)){
            woAttachmentList = woAttachmentMapper.selectList(new QueryWrapper<WoAttachment>()
                    .lambda().in(WoAttachment::getBussId, superviseIds));
        }
        List<WoAttachment> finalWoAttachmentList = woAttachmentList;
        list.stream().forEach(A->{
            WoDisregardKpiVO disregardKpiVO = new WoDisregardKpiVO();
            BeanUtils.copyProperties(A, disregardKpiVO);
            disregardKpiVO.setWoAttachmentList(finalWoAttachmentList.stream().filter(B->A.getId().equals(B.getBussId()))
                    .collect(Collectors.toList()));
            listVO.add(disregardKpiVO);
        });
        return AjaxResult.success(listVO);
    }

    /**
     * 导出不及考核申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:export')")
    @Log(title = "不及考核申请" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WoDisregardKpi disregardKpi) {
        List<WoDisregardKpi> list = disregardKpiService.selectListAll(disregardKpi);
        ExcelUtil<WoDisregardKpi> util = new ExcelUtil<WoDisregardKpi>(WoDisregardKpi.class);
        return util.exportExcel(list, "supervise");
    }

    /**
     * 获取不及考核申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(disregardKpiService.selectById(id));
    }

    /**
     * 新增不及考核申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:add')")
    @Log(title = "不及考核申请" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WoDisregardKpi disregardKpi) throws Exception {
        return disregardKpiService.insert(disregardKpi);
    }

    /**
     * 修改不及考核申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:edit')")
    @Log(title = "不及考核申请" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WoDisregardKpi disregardKpi) {
        return toAjax(disregardKpiService.update(disregardKpi));
    }

    /**
     * 删除不及考核申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:remove')")
    @Log(title = "不及考核申请" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(disregardKpiService.deleteByIds(ids));
    }

    /**
     * 获取不及考核申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:add')")
    @GetMapping(value = "/getInitSupervise")
    public AjaxResult getInitExtension() {
        Map<String, Object> result = new HashMap<>();
        // 获取登录人所在部门信息
        result.put("filledTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
        result.put("superviseNo", DateUtils.parseDateToStr("yyMMdd", new Date()));
        result.put("filledBy", SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success(result);
    }

    /**
     * 工作流程相关接口开始
     */
    /**
     * 分页查询工单信息我的代办列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @GetMapping("todoList")
    public AjaxResult todoList(WoDisregardKpiReqVO supervise) {
        List<WoDisregardKpiVO> list = disregardKpiService.selectTodoList(supervise);
        return getDataTable(list);
    }

    /**
     * 审核处理
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:add')")
    @Log(title = "审核处理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/approve")
    public AjaxResult approveExtension(@RequestBody Map<String, Object> maps) throws Exception {
        return disregardKpiService.approveSupervise(maps);
    }
}
